<template>
    <div class="sidebar">
        <el-menu
            class="sidebar-el-menu"
            :default-active="onRoutes"
            :collapse="collapse"
            text-color="#1523E4"
            active-text-color="#fff"
            unique-opened
            router
        >

            <!-- background-color="#324157 rgba(0,0,0,0.4) 20a0ff" -->
            <template v-for="item in items">
                <template v-if="item.subs">
                    <el-submenu :index="item.index" :key="item.index">
                        <template slot="title">
                            <i :class="item.icon"></i>
                            <span slot="title">{{ item.title }}</span>
                        </template>
                        <template v-for="subItem in item.subs">
                            <el-submenu
                                v-if="subItem.subs"
                                :index="subItem.index"
                                :key="subItem.index"
                            >
                                <template slot="title">
                                    <i :class="subItem.icon"></i>
                                    <span slot="title">{{ subItem.title }}</span>
                                    <!-- {{ subItem.title }} -->
                                </template>
                                <el-menu-item
                                    v-for="(threeItem,i) in subItem.subs"
                                    :key="i"
                                    :index="threeItem.index"
                                >{{ threeItem.title }}</el-menu-item>
                            </el-submenu>
                            <el-menu-item
                                v-else
                                :index="subItem.index"
                                :key="subItem.index"
                            >
                                <template slot="title">
                                    <i :class="subItem.icon"></i>
                                    <span slot="title">{{ subItem.title }}</span>
                                    <!-- {{ subItem.title }} -->
                                </template>
                            </el-menu-item>
                        </template>
                    </el-submenu>
                </template>
                <template v-else>
                    <el-menu-item :index="item.index" :key="item.index">
                        <i :class="item.icon"></i>
                        <span slot="title">{{ item.title }}</span>
                    </el-menu-item>
                </template>
            </template>
        </el-menu>
    </div>
</template>

<script>
import bus from '../common/bus';
import request from '../../api/index';
export default {
    data() {
        return {
            collapse: false,
            items: [
            ]
        };
    },
    computed: {
        onRoutes() {
            // return this.$route.path
            const route = this.$route;
            const { meta, path } = route;
            // if set path, the sidebar will highlight the path you set
            if (meta.activeMenu) {
                return meta.activeMenu;
            }
            return path;
        }
    },
    created() {
        // 通过 Event Bus 进行组件间通信，来折叠侧边栏
        bus.$on('collapse', msg => {
            this.collapse = msg;
            bus.$emit('collapse-content', msg);
        });

        const accessRoutes = JSON.parse(localStorage.getItem('routers'));
        this.items = this.items.concat(this.setFun(accessRoutes));
    },
    methods:{
        setFun(arr, parentPath){
            const newArr = arr.map(item => {
              if(item.path === '/' && item.component === "Layout") {
                item = item.children[0]
                item.path = '/' + item.path
              }
                let cell = {
                    icon: `el-icon-${item.meta.icon}`,
                    index: parentPath ? `${parentPath}/${item.path}` : item.path,
                    title: item.meta.title
                }
                if(item.children && item.children.length > 0){
                    cell.subs = this.setFun(item.children, item.path)
                }
                return cell;
            })
            return newArr;
        }
    }
};
</script>

<style lang="scss" scoped>
.sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 101px;
    bottom: 0;
    overflow-y: scroll;
    background: url("~@/assets/img/sidebar_bg.png");
	background-size: 100% 100%;
  	::v-deep .el-menu-item,
	::v-deep .el-submenu__title {
		font-size: 20px;
		font-weight: 800;
		background-color: transparent;
		&:hover,
		&:focus {
			background-color: transparent;
		}
		i {
			color: #1523E4;
		}
		&.is-active {
			i {
				color: #fff;
			}
		}
	}
	::v-deep .el-menu {
		background-color: transparent;
	}
	::v-deep .el-submenu__title {
		background-color: transparent;
	}
}
.sidebar-el-menu{ opacity: 1;}
.sidebar::-webkit-scrollbar {
    width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
    width: 230px; background: none;
}
.sidebar > ul {
    height: 100%;
}
</style>
